$.zhh.events.on("/basedata/settings/area/", function(space, initial, extras) {

	var v = {};
	
	v.format = {
		changedAreaCode: function(value, data, index) {
			var area = data.newer || data.older;
			return area.areaCode;
		},
		changedChangeType: function(value, data, index) {
			var map = { CREATE:"新增", DELETE:"删除", UPDATE:"修改", RENEW:"迁移" };
			return map[value];
		},
		changedMessage: function(value, data, index) {
			var map = extras.AreaType.map;
			if (data.type == "CREATE") {
				return data.newerAreaText;
			} else if (data.type == "DELETE") {
				return data.olderAreaText;
			} else if (data.type == "RENEW") {
				return data.olderAreaText + "(" + data.older.areaCode + ")" + " 变更为 " + data.newerAreaText + "(" + data.newer.areaCode + ")";
			} else if (data.type == "UPDATE") {
				var buffer = [];
				if (data.older.areaName != data.newer.areaName) {
					buffer.push("全称: " + data.older.areaName + " 变更为 " + data.newer.areaName);
				}
				if (data.older.shortName != data.newer.shortName) {
					if (!data.older.shortName) {
						buffer.push("增加简称: " + data.newer.shortName);
					} else if (!data.newer.shortName) {
						buffer.push("删除简称: " + data.older.shortName);
					} else {
						buffer.push("简称: " + data.older.shortName + " 变更为 " + data.newer.shortName);
					}
				}
				if (data.older.type != data.newer.type) {
					buffer.push("类型: " + data.older.type + " 变更为 " + data.newer.type);
				}

				if (data.older.parentCode != data.newer.parentCode) {
					var text = data.olderAreaText + " 变更为 " + data.newerAreaText;
					if (buffer.length > 0) { text += "; " + buffer.join("; "); }
					return text;
				} else {
					if (buffer.length > 0) {
						return data.olderAreaText + " -- " + buffer.join("; ");
					}
				}
			}
		}
	};

	v.onBeforeFillDataOfTest = function(e) {
		var me = e.form;
		if (!me.data("AreaDivisionInit")) {
			me.data("AreaDivisionInit", true);
			me.find(".area-division-box").areaChoose({
				fixedParams: { sceneType:"default" }
			});
		}
		setTimeout(function() { me.find("input[name=areaText]").focus(); }, 100);
	};
	v.onBeforeFillDataOfSync = function(e) {
		var dlg = e.popup;
		dlg.find(".sync-prompt").find(".before").removeClass("hide").siblings().addClass("hide");
		var buttons = dlg.siblings(".dialog-button");
		if (buttons.length > 0) {
			buttons.find("a.do-compare").xcall("enable");
			buttons.find("a.do-save").xcall("disable");
		}
	};
	v.onClickOfStartCompare = function(e) {
		var dlg = e.popup;
		var btn = $(this);
		var bopts = btn.xcall("options");
		dlg.find(".sync-prompt").find(".compare-doing").removeClass("hide").siblings().addClass("hide");
		btn.zajax(bopts.url, initial.fixedParams, {
			succTips: false,
			succ: function(json) {
				dlg.siblings(".dialog-button").find("a.do-save").xcall("enable");
				if (json.body && json.body.length > 0) {
					dlg.find(".sync-prompt").find(".after").removeClass("hide").siblings().addClass("hide");
					dlg.find(".x-datagrid").xcall("loadData", json.body);
				} else {
					dlg.find(".sync-prompt").find(".empty").removeClass("hide").siblings().addClass("hide");
					dlg.find(".x-datagrid").xcall("loadData", []);
				}
			}
		});
		return false; // 不关闭对话框
	};
	v.onClickOfSaveChanged = function(e) {
		var dlg = e.popup;
		var btn = $(this);
		var bopts = btn.xcall("options");
		var table = dlg.find(".x-datagrid");
		var data = table.xcall("getData").rows;
		var params = $.extend(true, {}, initial.fixedParams);
		// params.changed = JSON.stringify(data);
		dlg.find(".sync-prompt").find(".save-doing").removeClass("hide").siblings().addClass("hide");
		btn.zajax(bopts.url, params, {
			succ: function(json) {
				dlg.xcall("close");
			}, fail: function() {
				dlg.find(".sync-prompt").find(".after").removeClass("hide").siblings().addClass("hide");
			}
		});
		return false; // 不关闭对话框
	};

	space.xui(v);
});
$.extend($.fn.validatebox.defaults.rules, {
	areaCode : { // 编号
		validator : function(value) {
			return /^[0-9]{6}$/i.test(value);
		},
		message : "请输入6位数字"
	}
});